package com.airsystem.pos.bengkel.model.dialog;

import java.awt.GridBagLayout;
import java.math.BigDecimal;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import com.airsystem.pos.bengkel.component.DialogFrame;
import com.airsystem.pos.bengkel.entity.DetailKendaraan;
import com.airsystem.pos.bengkel.entity.DetailSparepart;
import com.airsystem.pos.bengkel.entity.JenisSparepart;
import com.airsystem.pos.bengkel.entity.Kendaraan;
import com.airsystem.pos.bengkel.entity.Sparepart;
import com.airsystem.pos.bengkel.util.TextUtil;

/**
 * @author Fanny Irawan Sutawanir (fannyirawans@gmail.com)
 */
public class MasterSparepartDialogModel extends DialogFrame {
	protected int row = 0;

	private static final String[] COLUMNS = { "Merk", "Sub Merk", "Merk Kendaraan",
											  "Sub Merk Kendaraan", "Jenis Sparepart", "Harga Satuan",
											  "Stok", "ID" };

	protected static final String[] COMBOMENU = { "-- PILIH --", "Merk", "Sub Merk",
												  "Merk Kendaraan", "Sub Merk Kendaraan", "Jenis Sparepart",
												  "Harga", "Stok" };

	protected final GridBagLayout baseLayout = new GridBagLayout();
	protected final GridBagLayout tableLayout = new GridBagLayout();

	protected final JPanel basePanel = new JPanel();
	protected final JPanel tablePanel = new JPanel();

	protected final JScrollPane tableScroll = new JScrollPane();

	protected final JTable tableData = new JTable();

	protected final JLabel searchLabel = new JLabel();

	protected final JComboBox searchCombo = new JComboBox();

	protected final JTextField searchField = new JTextField();

	protected final JButton searchButton = new JButton();

	protected final DefaultTableModel tableModel = new DefaultTableModel(new Object[][] {}, COLUMNS) {

		final boolean[] canEdit = new boolean[] { false, false, false,
												  false, false, false,
												  false, false };

		public boolean isCellEditable(int rowIndex, int columnIndex) {
			return canEdit[columnIndex];
		}
	};

	private DetailSparepart detailSparepart;

	public DetailSparepart getDetailSparepart() {
		return detailSparepart;
	}

	public void setDetailSparepart(DetailSparepart detailSparepart) {
		this.detailSparepart = detailSparepart;
	}

	protected DetailSparepart getComponentData() {
		DetailSparepart jenisTagihan = new DetailSparepart();

		return jenisTagihan;
	}

	protected DetailSparepart getSearchData() {
		DetailSparepart detailspSearch = new DetailSparepart();
		detailspSearch.setChoice(searchCombo.getSelectedIndex());

		String input = searchField.getText().trim();

		switch (searchCombo.getSelectedIndex()) {
		case 1: {
			detailspSearch.setSparepart(new Sparepart());
			detailspSearch.getSparepart().setMerk(input);
			break;
		}
		case 2: {
			detailspSearch.setSub_merk(input);
			break;
		}
		case 3: {
			detailspSearch.setDetail_kendaraan(new DetailKendaraan());
			detailspSearch.getDetail_kendaraan().setKendaraan(new Kendaraan());
			detailspSearch.getDetail_kendaraan().getKendaraan().setMerk(input);
			break;
		}
		case 4: {
			detailspSearch.setDetail_kendaraan(new DetailKendaraan());
			detailspSearch.getDetail_kendaraan().setSub_merk(input);
			break;
		}
		case 5: {
			detailspSearch.setJenis_sparepart(new JenisSparepart());
			detailspSearch.getJenis_sparepart().setNama(input);
			break;
		}
		case 6: {
			if (!TextUtil.isNumberOnly(input)) {
				throw new IllegalArgumentException("Wrong chosen search combo");
			}

			detailspSearch.setHarga_satuan(new BigDecimal(input));
			break;
		}
		case 7: {
			if (!TextUtil.isNumberOnly(input)) {
				throw new IllegalArgumentException("Wrong chosen search combo");
			}

			detailspSearch.setStok(Long.parseLong(input));
			break;
		}

		default:
			throw new IllegalArgumentException("Wrong chosen search combo");
		}

		return detailspSearch;
	}
}